<?php
/**
 * Date: 2024.8
 * Author: kahn
 */
namespace service\uc\oauth;

 abstract  class BaseAuthClient {

     protected $config;

     private $userAgent= "Auth client";
     private $timeout = 30;
     private $connectTimeout=30;


     public function __construct($config)
     {
         $this->config = $config;
     }


     /**
      * HTTP POST
      * @param  string 	$url    要请求的url地址
      * @param  array 	$params 请求的参数
      * @return string
      */
     public function postRequest($url, $params)
     {
         $curl = curl_init();

         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
         curl_setopt($curl, CURLOPT_USERAGENT, $this->userAgent);
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
         curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_HEADER, 0);
         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
         curl_setopt($curl, CURLOPT_URL, $url );

         // curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE );

         $response = curl_exec($curl);

         curl_close($curl);

         return $response;
     }

     public function getRequest($url, $params)
     {

         $curl = curl_init();

         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
         curl_setopt($curl, CURLOPT_USERAGENT, $this->userAgent);
         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
         curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curl, CURLOPT_HEADER, 0);

         $url = $url . '?' . http_build_query($params);
         curl_setopt($curl, CURLOPT_URL, $url );

         $response = curl_exec($curl);

         curl_close($curl);

         return $response;
     }

     protected function ToUrlParams($urlObj)
     {
         $buff = "";
         foreach ($urlObj as $k => $v)
         {
             if($k != "sign"){
                 $buff .= $k . "=" . $v . "&";
             }
         }

         $buff = trim($buff, "&");
         return $buff;
     }

 }